package com.laizhenghua.share.device.controller;

import com.laizhenghua.share.common.core.domain.R;
import com.laizhenghua.share.common.core.web.controller.BaseController;
import com.laizhenghua.share.common.core.web.page.TableDataInfo;
import com.laizhenghua.share.common.core.web.page.TableSupport;
import com.laizhenghua.share.device.domain.entity.CabinetType;
import com.laizhenghua.share.device.service.ICabinetTypeService;
import com.share.common.security.annotation.RequiresPermissions;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * TODO
 *
 * @Description
 * @Author laizh
 * @Date 2025/6/22 18:01
 **/
@Tag(name = "柜机类型API")
@RestController
@RequestMapping("/cabinet-type")
public class CabinetTypeController extends BaseController {
    private final ICabinetTypeService iCabinetTypeService;

    @Autowired
    public CabinetTypeController(ICabinetTypeService iCabinetTypeService) {
        this.iCabinetTypeService = iCabinetTypeService;
    }

    @RequiresPermissions(value = {"device:cabinet-type:query"})
    @Operation(summary = "分页查询")
    @PostMapping("/search-page/list")
    @Parameters({
            @Parameter(name = TableSupport.PAGE_NUM, description = "分页索引", example = "1"),
            @Parameter(name = TableSupport.PAGE_SIZE, description = "分页大小", example = "10")
    })
    @io.swagger.v3.oas.annotations.parameters.RequestBody(content = {@Content(schema = @Schema(implementation = CabinetType.class))})
    public R<TableDataInfo> searchPageList(@RequestBody CabinetType cabinetType) {
        startPage(); // 分页参数封装
        List<CabinetType> list = iCabinetTypeService.searchPageList(cabinetType);
        return R.ok(getDataTable(list));
    }

    @GetMapping("/{id}")
    @Operation(summary = "根据id获取详情")
    @Parameter(name = "id", description = "主键")
    public R<CabinetType> getDetailById(@PathVariable Long id) {
        return R.ok(iCabinetTypeService.getById(id));
    }

    @RequiresPermissions(value = {"device:cabinet-type:add"})
    @Operation(summary = "保存")
    @PostMapping("/save")
    public R<CabinetType> save(@RequestBody CabinetType cabinetType) {
        CabinetType s = iCabinetTypeService.customSave(cabinetType);
        return R.ok(s);
    }

    @Operation(summary = "删除")
    @DeleteMapping("/delete/{ids}")
    public R<String> deleteByIds(@PathVariable Long[] ids) {
        boolean b = iCabinetTypeService.removeBatchByIds(Arrays.asList(ids));
        return R.toR(b);
    }
}
